package com.jx.gn.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.jx.gn.config.LinkdoodProperties;
import com.jx.gn.entity.user.User;
import com.jx.gn.service.UserService;
import com.jx.gn.utils.HttpClientUtil;
import com.jx.gn.utils.JsonUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@AllArgsConstructor
@RequestMapping("login")
@Controller
@Slf4j
public class LoginController {
    private final WxMpService wxService;

    @Autowired
    private UserService userService;
    @Autowired
    private LinkdoodProperties linkdoodProperties;

    //    @GetMapping
//    private String index() {
//
//        return "login";
//    }
    @GetMapping
    private String index(@RequestParam String code, Model model) {

        try {
            WxMpOAuth2AccessToken accessToken = wxService.oauth2getAccessToken(code);
            WxMpUser user = wxService.oauth2getUserInfo(accessToken, null);
//            String str = "{\"openId\":\"o18NK6MPX1OpxYIwDivt4ZvvvsBU\",\"nickname\":\"宋晶磊\",\"sexDesc\":\"男\",\"sex\":1,\"language\":\"zh_CN\",\"city\":\"漯河\",\"province\":\"河南\",\"country\":\"中国\",\"headImgUrl\":\"http://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKPFYBIZYhJQics09PpiaGl0F7nac1YFE2HPDWNsZ4mlp5QDvUDt4GiagwmYHkL8wwvexNic4gQWzibrpA/132\",\"privileges\":[]}";
//            WxMpUser user = JSONUtil.toBean(JSONUtil.parseObj(str), WxMpUser.class);
            log.info("wxUser{}", user);
            User userByWxId = userService.getUserByWxId(user.getOpenId());
            if (null != userByWxId) {
                model.addAttribute("user", userByWxId);
            }

            model.addAttribute("wxUser", user);

        } catch (Exception e) {
            log.error("异常", e);
        }
        return "login";
    }


    @PostMapping("/bind")
    @ResponseBody
    private String bind(String carId, String openId) {
        log.info(carId);
        String lang = "zh_CN"; //语言
        try {

            String s = HttpClientUtil.doGetHeard(linkdoodProperties.getInfoUrl()+carId+"?lang=zh_CN");
            JSONObject json = JSONUtil.parseObj(s);
            if (json.getInt("code") != 0) {
                return json.getStr("msg");
            }
            WxMpUser user = wxService.getUserService().userInfo(openId, lang);

            User u = userService.getUserByWxId(openId);
            if (u == null) {

                int i = userService.saveUser(carId, user);
                if (i == 1) {
                    return "ok";
                }
            }
        } catch (Exception e) {
            log.error("用户绑定信息失败e{}", e);
        }


        return "fail";
    }

}
